﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Web.UI.WebControls;

namespace ASPNETVN.PORTAL.Components.Security
{
    public class SpecialRoles
    {
        #region SpecialPortalRoles enum

        public enum SpecialPortalRoles
        {
            [Description("All Users")]
            AllUsers = -1,
            [Description("Authenticated Users")]
            AuthenticatedUsers = -2,
            [Description("Unauthenticated Users")]
            UnauthenticatedUsers = -3
        }

        #endregion

        public static void PopulateSpecialRoles(ref CheckBoxList listRoles)
        {
            foreach (string s in Enum.GetNames(typeof(SpecialPortalRoles)))
            {
                SpecialPortalRoles desc = (SpecialPortalRoles)Enum.Parse(typeof(SpecialPortalRoles), s);
                string stringDesc = GetDescription(desc);
                listRoles.Items.Add(new ListItem(stringDesc, ((int)Enum.Parse(typeof(SpecialPortalRoles), s)).ToString()));
            }
        }

        public static string GetDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }

        public static string GetRoleName(int index)
        {
            string s = Enum.GetName(typeof(SpecialPortalRoles), index);
            SpecialPortalRoles desc = (SpecialPortalRoles)Enum.Parse(typeof(SpecialPortalRoles), s);
            return GetDescription(desc);
        }
    }
}